<?php
/**
 * @describe 验证测试
 *
 * @author wlx
 * @since 2018/11/14
 */

namespace App\Validates;


use Illuminate\Support\Facades\Validator;

class TestValidate
{

    /**
     * 错误信息
     * @var array
     */
    protected $errors = [];

    /**
     * 定义规则
     * @var array
     */
    protected $rules = [
        'id'   => 'required|numeric|min:1',
        'name' => 'required|max:200'
    ];

    /**
     * 定义消息提示
     * @var array
     */
    protected $messages = [
        'id.required' => 'ID不能为空111'
    ];

    /**
     * 构造函数
     *
     * @param $data
     */
    public function __construct($data)
    {
        $this->validate($data);
    }


    /**
     * 参数规则验证
     * @param array $data
     *
     * @return bool
     */
    public function validate($data)
    {
        $validate = Validator::make($data, $this->rules, $this->messages);
        if ($validate->fails()) {
            foreach ($validate->errors()->messages() as $key => $val) {
                if (isset($val[0])) {
                    $this->errors[$key] = $val[0];
                }
            }
        }
        return true;
    }

    /**
     * 获取错误信息
     * @return array
     */
    public function getErrors()
    {
        return $this->errors;
    }


}